<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>类型转换的判别</title>
</head>
<div id="layout">
  <div id="layout-content"><div id="language.types.type-juggling" class="sect1">
 <h2 class="title">类型转换的判别</h2>

 <p class="simpara">
  PHP 在变量定义中不需要（或不支持）明确的类型定义；变量类型是根据使用该变量的上下文所决定的。也就是说，如果把一个 <span class="type"><a href="language.types.string.html" class="type string">string</a></span> 值赋给变量
<var class="varname"><var class="varname">$var</var></var>，<var class="varname"><var class="varname">$var</var></var>
  就成了一个 <span class="type"><a href="language.types.string.html" class="type string">string</a></span>。如果又把一个<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> 赋给
  <var class="varname"><var class="varname">$var</var></var>，那它就成了一个<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>。
 </p>

 <p class="para">
  PHP 的自动类型转换的一个例子是乘法运算符“*”。如果任何一个操作数是<span class="type"><a href="language.types.float.html" class="type float">float</a></span>，则所有的操作数都被当成<span class="type"><a href="language.types.float.html" class="type float">float</a></span>，结果也是<span class="type"><a href="language.types.float.html" class="type float">float</a></span>。否则操作数会被解释为<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>，结果也是<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>。注意这并<em class="emphasis">没有</em>改变这些操作数本身的类型；改变的仅是这些操作数如何被求值以及表达式本身的类型。
 </p>

 <div class="informalexample">
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"1"</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;是字符串&nbsp;(ASCII&nbsp;49)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">*=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;现在是一个整数&nbsp;(2)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">1.3</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;现在是一个浮点数&nbsp;(2.6)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">5&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #DD0000">"10&nbsp;Little&nbsp;Piggies"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;是整数&nbsp;(50)<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">5&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #DD0000">"10&nbsp;Small&nbsp;Pigs"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;是整数&nbsp;(50)<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>

  </div>

 </div>

 <p class="simpara">
  如果上面两个例子看上去古怪的话，参见<a href="language.types.string.html#language.types.string.conversion" class="link">字符串转换为数值</a>。
 </p>

 <p class="simpara">
  如果要强制将一个变量当作某种类型来求值，参见<a href="language.types.type-juggling.html#language.types.typecasting" class="link">类型强制转换</a>一节。如果要改变一个变量的类型，参见 <span class="function"><a href="settype.html" class="function">settype()</a></span>。
 </p>

 <p class="para">
  如果想要测试本节中任何例子的话，可以用 <span class="function"><a href="var_dump.html" class="function">var_dump()</a></span> 函数。
 </p>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   自动转换为 <span class="type"><span class="type 数组">数组</span></span> 的行为目前没有定义。
  </p>

  <p class="para">
   此外，由于 PHP 支持使用和数组下标同样的语法访问字符串下标，以下例子在所有 PHP 版本中都有效：
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'car'</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$a&nbsp;is&nbsp;a&nbsp;string<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$a&nbsp;is&nbsp;still&nbsp;a&nbsp;string<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;bar<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="para">
   请参阅<a href="language.types.string.html#language.types.string.substr" class="link">存取和修改字符串中的字符</a>一节以获取更多信息。
  </p>
 </p></blockquote>

 <div class="sect2" id="language.types.typecasting">
  <h3 class="title">类型强制转换</h3>

  <p class="para">
   PHP 中的类型强制转换和 C 中的非常像：在要转换的变量之前加上用括号括起来的目标类型。
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;is&nbsp;an&nbsp;integer<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;(boolean)&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$bar&nbsp;is&nbsp;a&nbsp;boolean<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <p class="para">
   允许的强制转换有：
  </p>

  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">(int), (integer) - 转换为整形 <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span></span>
   </li>
   <li class="listitem">
    <span class="simpara">(bool), (boolean) - 转换为布尔类型 <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span></span>
   </li>
   <li class="listitem">
    <span class="simpara">(float), (double), (real) - 转换为浮点型 <span class="type"><a href="language.types.float.html" class="type float">float</a></span></span>
   </li>
   <li class="listitem">
    <span class="simpara">(string) - 转换为字符串 <span class="type"><a href="language.types.string.html" class="type string">string</a></span></span>
   </li>
   <li class="listitem">
    <span class="simpara">(array) - 转换为数组 <span class="type"><a href="language.types.array.html" class="type array">array</a></span></span>
   </li>
   <li class="listitem">
    <span class="simpara">(object) - 转换为对象 <span class="type"><a href="language.types.object.html" class="type object">object</a></span></span>
   </li>
   <li class="listitem">
    <span class="simpara">(unset) - 转换为 <span class="type"><a href="language.types.null.html" class="type NULL">NULL</a></span> (PHP 5)</span>
   </li>
  </ul>

  <p class="para">
   (binary) 转换和 b 前缀转换支持为 PHP 5.2.1 新增。
  </p>

  <p class="para">
   注意在括号内允许有空格和制表符，所以下面两个例子功能相同：
  </p>
   
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$foo&nbsp;</span><span style="color: #007700">=&nbsp;(int)&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;(&nbsp;int&nbsp;)&nbsp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>


   <p class="para">
    将字符串文字和变量转换为二进制字符串：
   </p>

   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$binary&nbsp;</span><span style="color: #007700">=&nbsp;(binary)</span><span style="color: #0000BB">$string</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$binary&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">b"binary&nbsp;string"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    可以将变量放置在双引号中的方式来代替将变量转换成字符串：
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$foo&nbsp;是一个整数<br /></span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$foo</span><span style="color: #DD0000">"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$str&nbsp;是一个字符串<br /></span><span style="color: #0000BB">$fst&nbsp;</span><span style="color: #007700">=&nbsp;(string)&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$fst&nbsp;也是一个字符串<br /><br />//&nbsp;输出&nbsp;"they&nbsp;are&nbsp;the&nbsp;same"<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$fst&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"they&nbsp;are&nbsp;the&nbsp;same"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p></blockquote>
   
  <p class="para">
   有时在类型之间强制转换时确切地会发生什么可能不是很明显。更多信息见如下小节：
  </p>
  
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.boolean.html#language.types.boolean.casting" class="link">转换为布尔型</a>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.integer.html#language.types.integer.casting" class="link">转换为整型</a>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.float.html#language.types.float.casting" class="link">转换为浮点型</a>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.string.html#language.types.string.casting" class="link">转换为字符串</a>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.array.html#language.types.array.casting" class="link">转换为数组</a>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.object.html#language.types.object.casting" class="link">转换为对象</a>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.resource.html#language.types.resource.casting" class="link">转换为资源</a>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="language.types.null.html#language.types.null.casting" class="link">转换为 NULL</a>
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <a href="types.comparisons.html" class="link">类型比较表</a>
    </span>
   </li>
  </ul>

 </div>
</div></div></div></body></html>